from property import Property

class StringProperty(Property):
    ''' Represents variable length strings.
    
        String properties are indexed as ``bin``.
    '''
    def __init__(self, **kwargs):
        kwargs['type'] = str
        super(StringProperty, self).__init__(**kwargs)
   
    def get_index_types(self):
        return ['bin']
        
    def get_index_keys(self):
        return ['%s_bin' % self.get_id()]
     
    def encode_index_values(self, v):
        return [v]
        
    def encode_single(self, v):
        if not isinstance(v, basestring):
            raise ValueError("StringProperty property %s value must be a basestr. Got %s" % (self.get_id(), type(v)))
        return v
        
    def encode_other(self, other):
        if isinstance(other, unicode):
            return other.encode('utf-8')
        elif isinstance(other, str):
            return other
        else:
            raise ValueError("Filter value must be a str or unicode")
            
    def encode_repeated(self, v):
        for i, val in enumerate(v):
            if not isinstance(val, basestring):
                raise ValueError("StringProperty property %s value must be a LIST of basestr. Got %s at %s" % (self.get_id(), type(val), i))
        return v

    def decode_single(self, v):
        return v
        
    def decode_repeated(self, v):
        return v
